package com.javandad.app.client.project;

import java.util.List;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.DecoratedTabPanel;
import com.google.gwt.user.client.ui.RootPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Project implements EntryPoint {
	public final static int NUMBER_OF_TABS = 4;

	private final ProjectServiceAsync projectService = GWT
			.create(ProjectService.class);

	private void initGUI(String projectName, String username, boolean owner) {
		RootPanel container = RootPanel.get("ProjectContainer");
		if (container == null)
			return;

		tabPanel.setAnimationEnabled(true);

		tabPanel.setWidth("100%");
		tabPanel.setHeight("600px");

		tabs = new AbsolutePanel[NUMBER_OF_TABS];

		tabs[0] = new HomeTab(projectService, projectName, username, owner);
		tabs[1] = new WallTab(projectService, projectName, username, owner);
		tabs[2] = new AssociatesTab(projectService, projectName, username, owner);
		tabs[3] = new UpdateTab();

		tabPanel.add(tabs[0], "Home");
		tabPanel.add(tabs[1], "Wall");
		tabPanel.add(tabs[2], "Associates");
		//tabPanel.add(tabs[3], "Update");

		tabPanel.selectTab(0);

		tabPanel.addSelectionHandler(new SelectionHandler<Integer>() {

			@Override
			public void onSelection(SelectionEvent<Integer> event) {
				int tab = event.getSelectedItem();
				switch (tab) {
				case 0:
					if (session != null && session.get(1) != null) {
					}
					break;
				case 1:
					if (session != null && session.get(1) != null) {
					}
					break;

				case 3:
					if (session != null && session.get(1) != null) {
					}
					break;
				}
			}

		});

		container.add(tabPanel);

	}

	@Override
	public void onModuleLoad() {
        projectName = com.google.gwt.user.client.Window.Location.getParameter("name");
		getSessionInfo();

	}

	private void getSessionInfo() {
		projectService.getSessionInfo(projectName, new AsyncCallback<List<String>>() {

			@Override
			public void onFailure(Throwable caught) {
			}

			@Override
			public void onSuccess(List<String> result) {
				session = result;
				username = result.get(1);
				owner = Boolean.parseBoolean(result.get(2));
				
				initGUI(projectName, username, owner);

			}

		});
	}

	private final DecoratedTabPanel tabPanel = new DecoratedTabPanel();
	private AbsolutePanel[] tabs = null;

	//LinkedList<HorizontalPanel> notis = new LinkedList<HorizontalPanel>();

	List<String> session = null;
	private String username;
	private String projectName;
	private boolean owner = false;
}
